<template>
    <page-container bgColor="#F4F4F4" :tabShow='true' @lowerCall='lowerCall'>
        <cu-custom bgColor="bg-white" :isBack="true">
            <block slot="backText"></block>
            <block slot="content">index</block>
        </cu-custom>
        <view slot="center">
            <!-- <view class="head" style="background-image: url(https://xmgts.kanfoshan.cn/public/static/images/head_bg.png)"> -->
			<view class="head" style="background-image: url(../../static/images/head_bg.png)">
                <view class="head_welcome" v-if="userInfo.status == 1">{{userInfo.userName}}，{{hello}}</view>
                <view class="head_apply" v-if="userInfo.status !== 1">
                    <view class="apply_buttom" @click="apply">点击申请</view>
                </view>
                <view class="head_enterprise" v-if="userInfo.status !== 1">申请成为企业购会员，尊享多重权益</view>
                <view class="head_enterprise" v-if="userInfo.status == 1">{{userInfo.enterprise}}</view>
                <view class="head_member">
                    <view class="member_title">企业会员</view>
                    <view class="member_save">累计节省{{(userInfo.price).toFixed(2)}}元</view>
                </view>
            </view>

            <!-- 企业会员专享权益 -->
            <view class="interest">
                <view class="interest_title">企业会员专享权益</view>
                <view class="interest_list">
                    <view class="list_img" v-for="(item,index) in interestList" :key="index">
                        <image :src="item.imgURL"></image>
                    </view>
                </view>
            </view>

            <!-- 微页面 -->
            <diy-group :diyData="diyData" v-if="diyData.value"></diy-group>

            <!-- 遮罩层 -->
            <view class="shade" v-if="sortStatus == 2"></view>
            <!-- 吸顶盒子 -->
            <view class="ceilingbox">
                <!-- 类型选项栏 -->
                <view class="typefence">
                    <view :class="{typefence_active:item.id == typeId,typefence_type:true}" v-for="(item,index) in typeList" :key="index" @click="changeType(item.id)">{{item.name}}
                    </view>
                </view>
                <!-- 筛选栏 -->
                <view class="screenlist">
                    <view class="screenlist_item" @click="changePrice()">
                        <view class="item_title">价格</view>
                        <image :src="$util.imgPath('up.png')" :class="{item_choose:priceStatus == 'desc'}"></image>
                    </view>
                    <view class="screenlist_item" @click="changeScreen()">
                        <view class="item_title">销量</view>
                        <image :src="$util.imgPath('up.png')" :class="{item_choose:sellStatus == 'desc'}"></image>
                    </view>
                    <view class="screenlist_item" @click="changeSort()">
                        <view :class="{item_title:true,item_active:sortStatus == 2}">分类</view>
                        <image :src="$util.imgPath('up.png')" class='item_choose' v-if="sortStatus == 1"></image>
                        <image :src="$util.imgPath('choose_active.png')" v-if="sortStatus == 2"></image>
                    </view>
                    <!-- 分类下拉选项框 -->
                    <view class="sortselect" v-if="sortStatus == 2">
                        <scroll-view scroll-y="true" style="height: 500rpx;">
                            <view class="sortselect_item" v-for="(item,index) in selectList" :key="index" @click="changeSelect(item.category_id)">
                                <view class="item_title">{{item.category_name}}</view>
                                <image :src="$util.imgPath('gou.png')" v-if="item.status == 2"></image>
                                <view class="item_circular" v-if="item.status == 1"></view>
                            </view>
                        </scroll-view>
                        <view class="sortselect_confirm">
                            <view class="confirm_title" @click="selectConfirm()">确定</view>
                        </view>
                    </view>
                </view>
            </view>
            <!-- 商品栏 -->
            <view class="goodslist">
                <view class="goodslist_li" v-for="(item,index) in goodsList" :key="index" @click="toDetail(item.sku_id)">
                    <view class="li_img">
                        <image :src="item.sku_image"></image>
                    </view>
                    <view class="li_title">{{item.sku_name}}</view>
                    <view class="li_original">￥{{item.price}} 醒目价</view>
                    <view class="li_discount">
                        <view class="">￥{{item.member_price}}</view>
                        <image :src="$util.imgPath('price.png')"></image>
                    </view>
                </view>
            </view>
            <ns-login ref="login"></ns-login>
        </view>
    </page-container>
</template>

<script>
export default {
	data() {
		return {
			userInfo: {
				status: 1, // 审核状态 0 未审核，1通过，2驳回, -1未申请
				enterprise: '',
				userName: '',
				price: 0
			},
			hello: '',
			interestList: [
				{ imgURL: 'https://xmgts.kanfoshan.cn/public/static/images/item1.png' },
				{ imgURL: 'https://xmgts.kanfoshan.cn/public/static/images/item2.png' },
				{ imgURL: 'https://xmgts.kanfoshan.cn/public/static/images/item3.png' },
				{ imgURL: 'https://xmgts.kanfoshan.cn/public/static/images/item4.png' },
				{ imgURL: 'https://xmgts.kanfoshan.cn/public/static/images/item5.png' }
			],
			goodsList: [],
			typeList: [],
			typeId: 0,
			priceStatus: 'desc', // asc:价格升序 desc：价格降序
			sellStatus: 'desc', // asc:销量升序 desc：销量降序
			sortStatus: 1,
			selectList: [{ category_id: 0, category_name: '不限', status: 2 }],
			diyData: {},
			token: '',
			page: 1
		}
	},
	onShow() {
		this.getUserToken()
		this.getEnterpriseTabs()
		this.getViewInfo()
		this.getTimeState()
	},
	methods: {
		lowerCall() {
			console.log('触底了')
			this.page += 1
			this.getEnterpriseGoodsList()
		},
		toDetail(id) {
			console.log(id)
			this.$util.redirectTo('/pages/goods/detail/detail', {
				sku_id: id
			})
		},
		/**
             * getUserToken
             * 获取用户token
             */
		getUserToken() {
			this.token = uni.getStorageSync('token')
			this.getEnterpriseStatus()
		},

		/**
             * getEnterpriseStatus
             * 申请企业购状态
             */
		getEnterpriseStatus() {
			this.$request
				.getEnterpriseStatus({
					token: this.token
				}).then(res => {
					console.log('getEnterpriseStatus', res)
					if (res.data == null) {
						this.userInfo.status = -1
					} else {
						this.userInfo.status = res.data.corp_status
						if (res.data.corp_status == 1) {
							this.userInfo.userName = res.data.corp_nickname
							this.userInfo.enterprise = res.data.corp_name
							this.getEconomy()
						}
					}
					console.log('userInfo', res)
				})
		},

		/**
             * getEconomy
             * 获取节省金额
             */
		getEconomy() {
			this.$request
				.getEconomy({
					token: this.token
				}).then(res => {
					console.log('getEconomy', res)
					this.userInfo.price = res.data
				})
		},

		/**
             * getViewInfo
             * 获取自定义页面组件
             */
		async getViewInfo() {
			let {
				data
			} = await this.$request.diyViewInfo({
				name: 'DIY_VIEW_RANDOM_1637207403',
				website_id: 0
			})
			if (data.value) {
				this.diyData = JSON.parse(data.value)
			}
		},

		/**
             * getEnterpriseTabs
             * 首页获取企业标签接口（无需参数）
             */
		getEnterpriseTabs() {
			this.$request
				.getEnterpriseTabs().then(res => {
					this.typeList = res.data
					this.typeId = res.data[0].id
					this.getScreenList()
				})
		},

		/**
             * getEnterpriseTabs
             * 首页获取商品分类列表
             */
		getScreenList() {
			this.$request
				.getScreenList({
					level: 1
				}).then(res => {
					res.data.map(obj => {
						obj.status = 1
					})
					this.selectList = this.selectList.concat(res.data)
					this.getEnterpriseGoodsList()
				})
		},

		/**
         * getEnterprise
         * 获取企业购商品列表
         */
		getEnterpriseGoodsList() {
			// 获取筛选中选中的ID
			var selectID = this.selectList.map(obj => {
				if (obj.status == 2) {
					return obj.category_id
				}
			})
			selectID = selectID.filter(item => item)
			var screenId = selectID.join(',')
			// 获取接口数据
			this.$request
				.getEnterpriseGoodsList({
					lable_id: this.typeId,
					category_id: screenId,
					sale_num: this.sellStatus,
					discount_price: this.priceStatus,
					page: this.page,
					page_size: 10,
					token: this.token
				}).then(res => {
					var data = res.data.list
					if (!this.token) {
						data = data.map(item => {
							item.member_price = '请先登录'
						})
					}
					if (this.page == 1) {
						this.goodsList = res.data.list
					} else {
						this.goodsList = this.goodsList.concat(res.data.list)
					}
					console.log(this.page)
					console.log('getEnterpriseGoodsList', this.goodsList)
				})
		},

		/**
             * apply
             * 企业购申请
             */
		apply() {
			if (!uni.getStorageSync('token')) {
				this.$refs.login.open('/pages/enterprisePurchase/index')
				return
			}

			if (this.userInfo.status == -1) {
				this.$util.redirectTo('/subpackage/enterprisePurchase/enterprisePurchase')
			} else if (this.userInfo.status == 0 || this.userInfo.status == 2) {
				this.$util.redirectTo('/subpackage/enterprisePurchase/apply')
			}
		},

		// 类型栏切换
		changeType(id) {
			if (this.typeId !== id) {
				this.typeId = id
				this.page = 1
				this.getEnterpriseGoodsList()
			}
		},
		// 价格排序切换
		changePrice() {
			if (this.priceStatus == 'asc') {
				this.priceStatus = 'desc'
				this.page = 1
			} else {
				this.priceStatus = 'asc'
				this.page = 1
			}
			this.getEnterpriseGoodsList()
		},
		// 销量排序切换
		changeScreen() {
			if (this.sellStatus == 'asc') {
				this.sellStatus = 'desc'
				this.page = 1
			} else {
				this.sellStatus = 'asc'
				this.page = 1
			}
			this.getEnterpriseGoodsList()
		},
		// 分类状态
		changeSort() {
			if (this.sortStatus == 1) {
				this.sortStatus = 2
			} else {
				this.sortStatus = 1
			}
		},
		// 选项栏选择
		changeSelect(id) {
			if (id == 0) {
				if (this.selectList[0].status == 2) {
					this.selectList[0].status = 1
				} else {
					this.selectList[0].status = 2
					this.selectList.map(obj => {
						if (obj.category_id !== 0) {
							obj.status = 1
						}
					})
				}
			} else if (this.selectList[0].status == 2) {
				this.selectList[0].status = 1
				this.selectList.map(obj => {
					if (obj.category_id == id) {
						if (obj.status == 1) {
							obj.status = 2
						} else {
							obj.status = 1
						}
					}
				})
			} else {
				this.selectList.map(obj => {
					if (obj.category_id == id) {
						if (obj.status == 1) {
							obj.status = 2
						} else {
							obj.status = 1
						}
					}
				})
			}
			this.getEnterpriseGoodsList()
		},
		// 选项栏确认
		selectConfirm() {
			this.sortStatus = 1
			this.page = 1
			this.getEnterpriseGoodsList()
		},
		/**
             * getTimeState
             * 判断目前时间早午晚
             */
		getTimeState() {
			// 获取当前时间
			let timeNow = new Date()
			// 获取当前小时
			let hours = timeNow.getHours()
			// 设置默认文字
			let state = ''
			// 判断当前时间段
			if (hours >= 0 && hours <= 10) {
				state = '上午好!'
			} else if (hours > 10 && hours <= 14) {
				state = '中午好!'
			} else if (hours > 14 && hours <= 18) {
				state = '下午好!'
			} else if (hours > 18 && hours <= 24) {
				state = '晚上好!'
			}
			this.hello = state
			console.log(state)
		}
	}
}
</script>

<style lang="scss" scoped>
.head {
    background-repeat: no-repeat;
    background-size: 100% 100%;
    padding: 0 31rpx;
    height: 419rpx;
    position: relative;

    &_welcome {
        color: #000000;
        font-size: 42rpx;
        font-weight: 800;
        position: absolute;
        top: 130rpx;
        left: 31rpx;
    }

    &_apply {
        width: 150rpx;
        height: 56rpx;
        line-height: 56rpx;
        background-color: #ffd332;
        border-radius: 28rpx;
        position: absolute;
        top: 130rpx;
        left: 31rpx;
        text-align: center;
        color: #222222;
        font-size: 28rpx;
        font-weight: bold;
    }

    &_enterprise {
        color: #999999;
        font-size: 28rpx;
        font-weight: 500;
        position: absolute;
        top: 200rpx;
        left: 31rpx;
    }

    &_member {
        width: 702rpx;
        padding: 0 50rpx;
        height: 110rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        position: absolute;
        bottom: 0;
        left: 24;
        color: #222222;
        font-size: 32rpx;
        font-weight: bold;
    }
}

.interest {
    width: 700rpx;
    padding: 0 25rpx;
    margin-top: 40rpx;
    margin-bottom: 26rpx;
    &_title {
        color: #222222;
        font-size: 30rpx;
        font-weight: bold;
    }
    &_list {
        width: 700rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .list_img {
            width: 133rpx;
            height: 164rpx;
            overflow: hidden;
            image {
                width: 133rpx;
                height: 164rpx;
            }
        }
    }
}

.shade {
    // 遮罩层的实现方法
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(36, 36, 36, 0.5);
    z-index: 99;
}
.ceilingbox {
    position: sticky;
    top: var(--window-top);
    z-index: 99;
    background-color: #f9f7fa;

    .typefence {
        display: flex;
        align-items: center;
        justify-content: space-between;
        text-align: center;

        &_type {
            width: 166rpx;
            height: 75rpx;
            line-height: 75rpx;
            font-size: 30rpx;
            color: #222222;
            font-weight: bold;
            margin-top: 27rpx;
        }

        &_active {
            color: #ffd332;
            border-bottom: 2rpx solid #ffd332;
        }
    }

    .screenlist {
        height: 64rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        position: relative;

        .sortselect {
            width: 100%;
            background-color: #f9f7fa;
            position: absolute;
            top: 64rpx;
            left: 0;

            &_item {
                width: 690rpx;
                height: 90rpx;
                display: flex;
                align-items: center;
                margin: 0 30rpx;
                justify-content: space-between;
                border-bottom: 1rpx solid #e9ebf3;

                image {
                    width: 30rpx;
                    height: 30rpx;
                }

                .item_circular {
                    width: 29rpx;
                    height: 29rpx;
                    border: 1rpx solid #666666;
                    border-radius: 50%;
                    overflow: hidden;
                }

                .item_title {
                    color: #222222;
                    font-size: 30rpx;
                    font-weight: 500;
                }
            }

            &_confirm {
                width: 100%;
                height: 114rpx;
                padding: 13rpx 40rpx;
                background-color: #ffffff;

                .confirm_title {
                    width: 670rpx;
                    height: 88rpx;
                    border-radius: 8rpx;
                    background-color: #ffd332;
                    text-align: center;
                    color: #222222;
                    font-size: 32rpx;
                    font-weight: 500;
                    line-height: 88rpx;
                }
            }
        }

        &_item {
            flex: 1;
            border: 1rpx solid #eeeeee;
            display: flex;
            align-items: center;
            justify-content: center;

            .item_title {
                color: #666666;
                font-size: 26rpx;
                font-weight: 500;
                margin-right: 10rpx;
            }

            .item_active {
                color: #ffd332;
            }

            .item_choose {
                transform: rotate(180deg);
            }

            image {
                width: 15rpx;
                height: 9rpx;
            }
        }
    }
}

.goodslist {
    width: 100%;
    padding: 34rpx 24rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;

    &_li {
        width: 340rpx;
        background-color: #ffffff;
        border-radius: 16rpx;
        overflow: hidden;
        margin-bottom: 20rpx;

        .li_img {
            width: 340rpx;
            height: 340rpx;
            overflow: hidden;

            image {
                width: 340rpx;
                height: 340rpx;
            }
        }

        .li_title {
            text-overflow: -o-ellipsis-lastline;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            -webkit-box-orient: vertical;
            padding: 0 17rpx;
            color: #222222;
            font-size: 24rpx;
            font-weight: 500;
        }

        .li_original {
            color: #a8a8a8;
            font-size: 24rpx;
            font-weight: 500;
            padding: 0 18rpx;
        }

        .li_discount {
            font-size: 32rpx;
            color: #000000;
            font-weight: bold;
            margin-left: 17rpx;
            display: flex;
            padding-bottom: 24rpx;

            image {
                width: 98rpx;
                height: 38rpx;
                margin-top: 10rpx;
            }
        }
    }
}
</style>

<style lang="scss" scoped>
</style>
